/*
 *功能:主消息类
 */

export class ptzCtrl {
    constructor(hConn_, ptzParam_) {
        this.hConn = hConn_;
        this.ptzPara = ptzParam_;
    }

    sendRequest() {
       var url = this.hConn.url;
       var wSock = new WebSocket(url);
       var revData;

       var self = this;

       var promise = new Promise(function (resolve , reject) {
           var tick = setTimeout(function () {
               resolve("timeout");
           }, 2000);

           wSock.onopen = ()=>{
               wSock.send(JSON.stringify({
                   command: "/ptzCtrl",
                   deviceHost: self.ptzPara.devip,
                   devicePort : self.ptzPara.devport,
                   channelNo: self.ptzPara.chn,
                   cmd: self.ptzPara.cmd,
                   preset: self.ptzPara.preset,
                   speed: self.ptzPara.speed,
                   stop:self.ptzPara.stop,
                   userKey: self.hConn.key,
                   version: "1.0"}));
           }

           wSock.onmessage = (e)=>{
               clearTimeout(tick);

               var blob = new Blob([e.data]);
               var reader = new FileReader();
               reader.readAsText(blob, 'utf-8');

               reader.onload = ()=>{
                   var obj = JSON.parse(reader.result);
                   resolve(obj);
               }
           }

           wSock.onclose = ()=>{

           }

           wSock.onerror = ()=>{

           }
       }).then(function (data) {
           wSock.close();
           revData = data;
      })

      return revData;
    }
}

export default  ptzCtrl;
